/**
 * 时间格式化
 * @param {Object} date
 */
export function formatDate(time) {
	if (!time) {
		return time
	}
	var thisTime = time;
	thisTime = thisTime.replace(/-/g, '/');
	var time2 = new Date(thisTime);
	var timestamp = time2.getTime();
	// 需不需要显示小时及分钟
	var mustIncludeTime = true;

	// 输入源时间
	var sourceDate = new Date(parseInt(timestamp));
	var sourceYear = sourceDate.getFullYear();
	var sourceMonth = (sourceDate.getMonth() + 1);
	var sourceDay = sourceDate.getDate();
	// 当前时间
	var currentDate = new Date();
	var currentYear = currentDate.getFullYear();
	var currentMonth = (currentDate.getMonth() + 1);
	var currentDateD = currentDate.getDate();

	// 返回的结果
	var resultDate = "";

	// 要额外显示的时间分钟
	var timeExtraStr = (mustIncludeTime ? " " + _formatDate(sourceDate, "hh:mm") : "");
	// 当年
	if (currentYear == sourceYear) {
		var currentTimestamp = currentDate.getTime();
		var sourceTimestamp = timestamp;
		// 相差时间（单位：毫秒）
		var deltaTime = (currentTimestamp - sourceTimestamp);

		// 当天（月份和日期一致才是）
		if (currentMonth == sourceMonth && currentDateD == sourceDay) {
			// 时间相差60秒以内
			if (deltaTime < 60 * 1000)
				resultDate = "刚刚";
			// 否则当天其它时间段的，直接显示“时:分”的形式
			else
				resultDate = _formatDate(sourceDate, "hh:mm");
		}
		// 当年 && 当天之外的时间（即昨天及以前的时间）
		else {
			// 昨天（以“现在”的时候为基准-1天）
			var yesterdayDate = new Date();
			yesterdayDate.setDate(yesterdayDate.getDate() - 1);

			// 前天（以“现在”的时候为基准-2天）
			var beforeYesterdayDate = new Date();
			beforeYesterdayDate.setDate(beforeYesterdayDate.getDate() - 2);

			// 用目标日期的“月”和“天”跟上方计算出来的“昨天”进行比较
			if (sourceMonth == (yesterdayDate.getMonth() + 1) && sourceDay == yesterdayDate.getDate())
				resultDate = "昨天" + timeExtraStr; // -1d
			else if (sourceMonth == (beforeYesterdayDate.getMonth() + 1) && sourceDay == beforeYesterdayDate.getDate())
				resultDate = "前天" + timeExtraStr; // -2d
			else {
				resultDate = _formatDate(sourceDate, "yyyy-M-d") + timeExtraStr;
				// // 跟当前时间相差的小时数
				// var deltaHour = (deltaTime / (3600 * 1000));

				// // 如果小于或等 7*24小时就显示星期几
				// if (deltaHour <= 7 * 24) {
				// 	var weekday = new Array(7);
				// 	weekday[0] = "星期日";
				// 	weekday[1] = "星期一";
				// 	weekday[2] = "星期二";
				// 	weekday[3] = "星期三";
				// 	weekday[4] = "星期四";
				// 	weekday[5] = "星期五";
				// 	weekday[6] = "星期六";
				// 	// 取出当前是星期几
				// 	var weedayDesc = weekday[sourceDate.getDay()];
				// 	resultDate = weedayDesc + timeExtraStr;
				// }
				// // 否则直接显示完整日期时间
				// else
				// 	resultDate = _formatDate(sourceDate, "yyyy-M-d") + timeExtraStr;
			}
		}
	}
	// 往年
	else {
		resultDate = _formatDate(sourceDate, "yyyy-M-d") + timeExtraStr;
	}

	return resultDate;
}

var _formatDate = function(date, fmt) {
	var o = {
		"M+": date.getMonth() + 1, //月份
		"d+": date.getDate(), //日
		"h+": date.getHours(), //小时
		"m+": date.getMinutes(), //分
		"s+": date.getSeconds(), //秒
		"q+": Math.floor((date.getMonth() + 3) / 3), //季度
		"S": date.getMilliseconds() //毫秒
	};
	if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (date.getFullYear() + "").substr(4 - RegExp.$1.length));
	for (var k in o)
		if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : ((
			"00" + o[
				k]).substr(("" + o[k]).length)));
	return fmt;
};

// 格式化时长
export function formattedDuration(seconds) {
	if(!seconds){
		return '00:00'
	}	
	const hours = Math.floor(seconds / 3600);
	const minutes = Math.floor((seconds % 3600) / 60);
	const remainingSeconds = seconds % 60;
	if (hours <1){
		return `${minutes.toString().padStart(2, '0')}:${remainingSeconds.toString().padStart(2, '0')}`;
	}
	
	return `${hours.toString().padStart(2, '0')}:${minutes.toString().padStart(2, '0')}:${remainingSeconds.toString().padStart(2, '0')}`;
}